using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace daq_test_cs
{
    public class TCPServerWrapper
    {
        private const string DllPath = "./libdaq-4.0.0.dll";

        /// <summary>
        /// 创建新的TCP服务器实例
        /// </summary>
        /// <param name="ip_address">服务器IP地址</param>
        /// <param name="ip_size">IP地址字符串长度</param>
        /// <param name="port">服务器端口</param>
        /// <returns>返回TCP服务器实例的指针，失败返回IntPtr.Zero</returns>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr TcpServer_New(string ip_address, int ip_size, ushort port);

        /// <summary>
        /// 删除TCP服务器实例，释放相关资源
        /// </summary>
        /// <param name="handle">TCP服务器实例的指针</param>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern void TcpServer_Delete(IntPtr handle);

        /// <summary>
        /// 开始接受客户端连接
        /// </summary>
        /// <param name="handle">TCP服务器实例的指针</param>
        /// <param name="duration_ms">接受连接的超时时间（毫秒）</param>
        /// <param name="async">是否异步接受连接</param>
        /// <param name="client_count">最大客户端连接数</param>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern void TcpServer_StartAccept(IntPtr handle, int duration_ms, bool async, uint client_count);

        /// <summary>
        /// 检查接受连接过程是否完成
        /// </summary>
        /// <param name="handle">TCP服务器实例的指针</param>
        /// <returns>如果接受连接过程完成，返回true；否则返回false</returns>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool TcpServer_IsAcceptComplete(IntPtr handle);

        /// <summary>
        /// 断开指定IP的客户端连接
        /// </summary>
        /// <param name="handle">TCP服务器实例的指针</param>
        /// <param name="ip">客户端IP地址</param>
        /// <param name="ip_size">IP地址字符串长度</param>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern void TcpServer_DisconnectClient(IntPtr handle, string ip, int ip_size);

        /// <summary>
        /// 获取所有已连接客户端的IP地址
        /// </summary>
        /// <param name="handle">TCP服务器实例的指针</param>
        /// <param name="client_count">输出参数，返回客户端数量</param>
        /// <returns>返回客户端IP地址数组的指针</returns>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr TcpServer_GetClientsIP(IntPtr handle, ref byte client_count);

        /// <summary>
        /// 根据IP地址获取客户端连接
        /// </summary>
        /// <param name="handle">TCP服务器实例的指针</param>
        /// <param name="ip">客户端IP地址</param>
        /// <param name="ip_size">IP地址字符串长度</param>
        /// <returns>返回客户端连接的指针，失败返回IntPtr.Zero</returns>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr TcpServer_GetClient(IntPtr handle, string ip, int ip_size);

        /// <summary>
        /// 将字符串指针数组转换为字符串列表
        /// </summary>
        /// <param name="strArrayPtr">字符串数组指针</param>
        /// <param name="count">数组长度</param>
        /// <returns>字符串列表</returns>
        public static List<string> GetStringListFromCharPtrArray(IntPtr strArrayPtr, int count)
        {            
            List<string> result = new List<string>();
            IntPtr[] ptrArray = new IntPtr[count];
            Marshal.Copy(strArrayPtr, ptrArray, 0, count);

            for (int i = 0; i < count; i++)
            {
                result.Add(Marshal.PtrToStringAnsi(ptrArray[i]));
            }

            return result;
        }
    }
}